package com.xthink.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xthink.common.constant.HttpStatus;
import com.xthink.common.result.AjaxResult;
import com.xthink.common.pojo.customer.Customer;
import com.xthink.common.vo.customer.*;
import com.xthink.common.pojo.user.User;
import com.xthink.web.mapper.CustomerMapper;
import com.xthink.web.mapper.UserMapper;
import com.xthink.web.service.CustomerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private UserMapper userMapper;


    //插入客户
    @Override
    public AjaxResult insertOne(CustomerInsertVo customerInsertVo) {
        if (!StringUtils.isNotBlank(customerInsertVo.getCustomerName())) {
            return AjaxResult.error("请输入客户公司");
        }

        if (!StringUtils.isNotBlank(customerInsertVo.getCity())) {
            return AjaxResult.error("请输入客户所在的城市");
        }

        if (!StringUtils.isNotBlank(customerInsertVo.getName())) {
            return AjaxResult.error("请输入客户姓名");
        }

        if (!StringUtils.isNotBlank(customerInsertVo.getPhone())) {
            return AjaxResult.error("请输入客户手机号");
        }

        Customer customer = new Customer();

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId, customerInsertVo.getSigning());
        User user = userMapper.selectOne(wrapper);
        String businessName = user.getBusinessName();

        BeanUtils.copyProperties(customerInsertVo, customer);
        customer.setSigning(businessName);
        int insert = customerMapper.insert(customer);


        if (insert == 0) {
            return AjaxResult.success("插入失败");
        }

        return AjaxResult.success("插入成功", customer);
    }


    //客户编辑
    @Override
    public AjaxResult updateOne(CustomerUpdateVo customerUpdateVo) {

        if (!StringUtils.isNotBlank(customerUpdateVo.getCustomerName())) {
            return AjaxResult.error("编辑失败", HttpStatus.BAD_REQUEST);
        }

        if (!StringUtils.isNotBlank(customerUpdateVo.getCity())) {
            return AjaxResult.error("编辑失败", HttpStatus.BAD_REQUEST);
        }

        if (!StringUtils.isNotBlank(customerUpdateVo.getName())) {
            return AjaxResult.error("编辑失败", HttpStatus.BAD_REQUEST);
        }

        if (!StringUtils.isNotBlank(customerUpdateVo.getPhone())) {
            return AjaxResult.error("编辑失败", HttpStatus.BAD_REQUEST);
        }
        UpdateWrapper<Customer> wrapper = new UpdateWrapper<>();

        LambdaQueryWrapper<User> wrapperUser = new LambdaQueryWrapper<>();
        wrapperUser.eq(User::getId, customerUpdateVo.getSigning());
        User user = userMapper.selectOne(wrapperUser);
        String businessName = user.getBusinessName();

        wrapper.eq("id", customerUpdateVo.getId())
                .set(StringUtils.isNotBlank(customerUpdateVo.getCustomerName()), "customer_name", customerUpdateVo.getCustomerName())
                .set(StringUtils.isNotBlank(customerUpdateVo.getCity()), "city", customerUpdateVo.getCity())
                .set(StringUtils.isNotBlank(customerUpdateVo.getName()), "name", customerUpdateVo.getName())
                .set(StringUtils.isNotBlank(customerUpdateVo.getPhone()), "phone", customerUpdateVo.getPhone())
                .set(StringUtils.isNotBlank(customerUpdateVo.getEmail()), "email", customerUpdateVo.getEmail())
                .set(customerUpdateVo.getSigning() != null, "signing", businessName)
                .set(StringUtils.isNotBlank(customerUpdateVo.getSale()), "sale", customerUpdateVo.getSale());
        int update = customerMapper.update(null, wrapper);
        if (update == 0) {
            return AjaxResult.error();
        }

        return AjaxResult.success();
    }

    //逻辑删除
    @Override
    public AjaxResult deleteOne(Integer id) {
        UpdateWrapper<Customer> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id).set("account_status", 0);
        int update = customerMapper.update(null, wrapper);
        if (update == 0) {
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    //账号生效
    @Override
    public AjaxResult updateTakeEffect(Integer[] ids) {

        for (Integer id : ids) {
            UpdateWrapper<Customer> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", id).set("status", 1);
            int total = customerMapper.update(null, wrapper);
            if (total == 0) {
                return AjaxResult.error("ID:" + id + "生效失败");
            }
        }
        return AjaxResult.success();
    }

    //账号失效
    @Override
    public AjaxResult updateInvalid(Integer[] ids) {
        for (Integer id : ids) {
            UpdateWrapper<Customer> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", id).set("status", 0);
            int total = customerMapper.update(null, wrapper);
            if (total == 0) {
                return AjaxResult.error("ID:" + id + "失效失败,该状态已是失效状态");
            }
        }
        return AjaxResult.success();
    }

    //模糊查询
    @Override
    public AjaxResult likeList(CustomerListVo customerListVo) {
        //模糊查询  条件封装
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        Page<Customer> page = new Page<>(customerListVo.getPage(), customerListVo.getSize());
        //客户名称
        wrapper.like(StringUtils.isNotBlank(customerListVo.getCustomerName()), Customer::getCustomerName, customerListVo.getCustomerName());
        //手机号
        wrapper.like(StringUtils.isNotBlank(customerListVo.getPhone()), Customer::getPhone, customerListVo.getPhone());
        //账户状态
        wrapper.eq(customerListVo.getStatus() != null, Customer::getStatus, customerListVo.getStatus());
        //签约主体
        wrapper.eq(StringUtils.isNotBlank(customerListVo.getSigning()), Customer::getSigning, customerListVo.getSigning());

        //责任销售
        wrapper.like(StringUtils.isNotBlank(customerListVo.getSale()), Customer::getSale, customerListVo.getSale());

        //禁用状态不显示
        wrapper.eq(Customer::getAccountStatus, 1);

        if (!StringUtils.isNotBlank(customerListVo.getCustomerName()) && !StringUtils.isNotBlank(customerListVo.getPhone()) && customerListVo.getSigning() != null && StringUtils.isNotBlank(customerListVo.getSale()) && customerListVo.getStatus() == null) {
            return AjaxResult.error("搜索失败", HttpStatus.BAD_REQUEST);
        }

        Page<Customer> customerPage = customerMapper.selectPage(page, wrapper);
        List<Customer> customers = customerPage.getRecords();

        if (customers.size() == 0) {
            return AjaxResult.error("未搜索到此客户");
        }

        return AjaxResult.success("查询成功", customers);
    }

    //分页查询
    @Override
    public AjaxResult pageInfo(CustomerPageVo customerPageVo) {
        Page<Customer> page = new Page<>(customerPageVo.getPage(), customerPageVo.getSize());
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        //禁用状态不显示
        wrapper.eq(Customer::getAccountStatus, 1);
        Page<Customer> userPage = customerMapper.selectPage(page, wrapper);
        List<Customer> customers = userPage.getRecords();

        return AjaxResult.success("查询成功", customers);
    }

    //签约主体下拉框
    @Override
    public AjaxResult signing() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getAccountStatus, 1);
        List<User> users = userMapper.selectList(wrapper);

        List<SigningVo> signing = new ArrayList<>();
        for (User user : users) {
            SigningVo signingVo = new SigningVo();
            signingVo.setId(user.getId());
            signingVo.setBusinessName(user.getBusinessName());
            signingVo.setSale(user.getName());
            signing.add(signingVo);
        }
        return AjaxResult.success(signing);
    }

}
